using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using Microsoft.SharePoint;
using Microsoft.SharePoint.StsAdmin;

namespace DD.SharePoint.STSADM.Extensions
{
    public class WebManagementCommand : ISPStsadmCommand
    {
        #region ISPStsadmCommand Members

        public string GetHelpMessage(string command)
        {
            command = command.ToLowerInvariant();

            switch (command)
            {
                case "emptyrecyclebin":
                    return "-url <Full url to a site in SharePoint>\n";
                case "enumfeatures":
                    return "-url <Full url to a site in SharePoint>\n";
                default:
                    throw new InvalidOperationException();
            }
        }

        public int Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
        {
            command = command.ToLowerInvariant();

            switch (command)
            {
                case "emptyrecyclebin":
                    return EmptyRecycleBin(keyValues, out output);
                case "enumfeatures":
                    return EnumFeatures(keyValues, out output);
                default:
                    throw new InvalidOperationException();
            }
        }

        private int EnumFeatures(StringDictionary keyValues, out string output)
        {
            if (!keyValues.ContainsKey("url"))
            {
                throw new InvalidOperationException("The url parameter was not specified");
            }

            StringBuilder sb = new StringBuilder();

            String url = keyValues["url"];
            SPWeb web = null;

            try
            {
                web = new SPSite(url).OpenWeb();
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Error retrieving url '" + url + "'.");
            }

            try
            {
                foreach (SPFeature feature in web.Features)
                {
                    sb.AppendLine(feature.Definition.DisplayName);
                }
                
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Operation failed");
            }

            output = sb.ToString();

            return 0;
        }

        private int EmptyRecycleBin(StringDictionary keyValues, out string output)
        {
            if (!keyValues.ContainsKey("url"))
            {
                throw new InvalidOperationException("The url parameter was not specified");
            }

            StringBuilder sb = new StringBuilder();

            String url = keyValues["url"];
            SPWeb web = null;

            try
            {
                web = new SPSite(url).OpenWeb();
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Error retrieving url '" + url + "'.");
            }

            try
            {
                web.RecycleBin.DeleteAll();

                sb.Append("Operation successfully completed");
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Operation failed");
            }

            output = sb.ToString();

            return 0;
        }

        #endregion
    }
}
